package source;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import entity.User;

import java.io.*;

/**
 * This simple tag implements header.
 * 
 * @author Qiu Zekang
 *
 */
public class UserPageHeaderTag extends TagSupport {
	/**
	 * 
	 */
	

	/**
	 * Rewrite doTag method to implement show footer in .JSP
	 * 
	 *
	 */
	public int doStartTag() throws JspException {

	String hh="\r\n";
	String picture="logo.png";//左上角图片文件名
	String shouYe_url="Index_Date";//首页的链接
	String geRenZhuYe_url;//首页的链接
	
	String welcome_word;//右上角的欢迎词语
	String welcome_color;//右上角欢迎词的颜色。如果用户没登陆，以红色的字提示用户该登陆了。
	String welcome_href;//右上角欢迎词点进去的链接：登陆 或者 跳转到个人主页
	String logout="";
	
	String fatie_href="BeforeAddArticle";//这里填写发帖跳转到的链接
	
	JspWriter out = pageContext.getOut();
	HttpSession session = pageContext.getSession();

	User user = (User) session.getAttribute("user");
	if(user==null) {//用户没登陆
		welcome_word="你好，请登录！";
		welcome_color="red";
		welcome_href="UserLogin";//这里填写登陆界面
		geRenZhuYe_url="UserLogin";//这里填写登陆界面
		fatie_href = "UserLogin";//这里填写登陆界面
		logout="";//用户没有登陆的话，没有退出的标题
	}else if(user.getUser_islocked()==0){//用户已登陆
		welcome_word="你好，"+user.getUser_name()+"!";
		welcome_color="black";
		welcome_href="My_Home";//这里填写登陆界面
		geRenZhuYe_url="My_Home";//这里填写登陆界面
		fatie_href="BeforeAddArticle";
		logout="<li><a href=\"LogOutServlet\">&nbsp;退出登录</a></li>";
	}else {
		welcome_word="你好，"+user.getUser_name()+"!";
		welcome_color="black";
		welcome_href="My_Home";//这里填写登陆界面
		geRenZhuYe_url="My_Home";//这里填写登陆界面
		fatie_href="User_lock_inf.jsp";
		logout="<li><a href=\"LogOutServlet\">&nbsp;退出登录</a></li>";
	}

	String outPrint = 
			"<header class=\"header\">"+hh+
			"<nav class=\"navbar navbar-default\" id=\"navbar\">"+hh+
			""+hh+
			"<div class=\"container\">"+hh+
			"<div class=\"header-topbar hidden-xs link-border\">"+hh+
			"<ul class=\"site-nav topmenu\">"+hh+
			"<li><a href=\""+welcome_href+"\" style=\"color:"+welcome_color+"\">"+welcome_word+"</a></li>"+hh+
			logout+
			"</ul>"+hh+
			"</div>"+hh+
			""+hh+
			"<div class=\"navbar-header\">"+hh+
			"<button type=\"button\" class=\"navbar-toggle collapsed\" data-toggle=\"collapse\" data-target=\"#header-navbar\" aria-expanded=\"false\"> <span class=\"sr-only\"></span> <span class=\"icon-bar\"></span> <span class=\"icon-bar\"></span> <span class=\"icon-bar\"></span> </button>"+hh+
			"<h1 class=\"logo hvr-bounce-in\"><a href=\"#\" title=\"企业论坛\"><img src=\"images/"+picture+"\" alt=\"企业论坛\"></a></h1>"+hh+
			"</div>"+hh+
			""+hh+
			"<div class=\"collapse navbar-collapse\" id=\"header-navbar\">"+hh+
			"<ul class=\"nav navbar-nav navbar-right\">"+hh+
			"<li><a data-cont=\"企业论坛主页\" title=\"论坛主页\" href=\""+shouYe_url+"\">首页</a></li>"+hh+
			"<li><a data-cont=\"个人主页\"title=\"个人主页\" href=\""+geRenZhuYe_url+"\">个人主页</a></li>"+hh+
			"<li><a class=\"glyphicon glyphicon-plus-sign\" data-cont=\"发帖\" title=\"发帖\" href=\""+fatie_href+"\">发帖</a></li>"+hh+
			"</ul>"+hh+
			"</div>"+hh+
			"</div>"+hh+
			"</nav>"+hh+
			"</header>"+hh;
			
		try {
			out.print(outPrint);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
}
